/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2021年11月2日
 */
package com.massyframework.beanskin.runtime.framework;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.massyframework.beanskin.assembly.AssemblyContext;
import com.massyframework.beanskin.assembly.AssemblyContextSupport;
import com.massyframework.beanskin.service.ServiceReference;
import com.massyframework.beanskin.service.ServiceRepository;
import com.massyframework.beanskin.service.ServiceTracker;
import com.massyframework.beanskin.service.ServiceTrackerCustomizer;
import com.massyframework.beanskin.util.classloading.ResourceLocateFilter;
import com.massyframework.beanskin.util.classloading.ResourceLocateFilterRepository;

/**
 * 缺省的资源定位过滤器资源库
 * @author huangkh
 */
final class DefaultResourceLocateFilterRepository extends AssemblyContextSupport
	implements ResourceLocateFilterRepository, ServiceTrackerCustomizer<ResourceLocateFilter> {
	
	private Logger logger = LoggerFactory.getLogger(DefaultResourceLocateFilterRepository.class);
	private Map<String, ResourceLocateFilter> filterMap;
	
	private ServiceTracker<ResourceLocateFilter> tracker;
  
	/**
	 * 构造方法
	 */
	public DefaultResourceLocateFilterRepository() {
		this.filterMap = new ConcurrentHashMap<String, ResourceLocateFilter>();
	}

	@Override
	public ResourceLocateFilter findResourceLocateFilter(String resourceName) {
		if (resourceName == null) return null;
		
		String name = resourceName.trim();
		return this.filterMap.get(name);
	}

	@Override
	public ResourceLocateFilter addService(ServiceReference<ResourceLocateFilter> reference,
			ServiceRepository repository) {
		ResourceLocateFilter result = repository.getService(reference).orElse(null);
		if (result != null) {
			String[] names = result.getResourceNames();
			
			for (String name: names) {
				if (this.filterMap.putIfAbsent(name, result) != null) {
					if (logger.isWarnEnabled()) {
						logger.warn("cannot add ResourceLocateFilter, because exists other: resourceName= " + name + ".");
					}
				}
			}
		}
		
		return result;
	}

	@Override
	public void removeService(ServiceReference<ResourceLocateFilter> reference, ResourceLocateFilter service) {		
		String[] names = service.getResourceNames();
		for (String name: names) {
			this.filterMap.remove(name, service);
		}
	}

	@Override
	protected synchronized void bindAssemblyContext(AssemblyContext context) {
		super.bindAssemblyContext(context);
		if (this.tracker == null) {
			this.tracker = new ServiceTracker<>(context.getAssociatedAssembly(), 
					ResourceLocateFilter.class, null, this);
			this.tracker.open();
		}
	}

	@Override
	protected synchronized void unbindAssemblyContext(AssemblyContext context) {
		if (this.tracker != null) {
			this.tracker.close();
			this.filterMap.clear();
			this.tracker = null;
		}
		super.unbindAssemblyContext(context);
	}
	
	
	
}
